package guestBook;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JPanel;

import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Update extends JInternalFrame {
	GuestBookMain gbm;
	private JTextField idIn;
	private JLabel label;
	private JLabel label_1;
	private JTextField subIn;
	private JScrollPane scrollPane;
	private JButton btnSearch;
	private JTextArea contentMsg;
	private JButton btnMod;
	private JButton btnDel;
	private JPanel panel_1;
	private JLabel lblNewLabel;
	private JTextField serialIn;

	public Update(GuestBookMain g) {
		super("방명록 수정 및 삭제",true,true,true,true);
		this.gbm=g;		
		setBounds(426, 0, 426, 450);
		getContentPane().setLayout(null);
		getContentPane().add(getIdIn());
		getContentPane().add(getLabel());
		getContentPane().add(getLabel_1());
		getContentPane().add(getSubIn());
		getContentPane().add(getScrollPane());
		getContentPane().add(getBtnSearch());
		getContentPane().add(getPanel_1());
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getSerialIn());

	}
	private JTextField getIdIn() {
		if (idIn == null) {
			idIn = new JTextField();
			idIn.setColumns(10);
			idIn.setBounds(76, 11, 101, 21);
		}
		return idIn;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("ID");
			label.setBounds(12, 14, 31, 15);
		}
		return label;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("Subject");
			label_1.setBounds(12, 45, 52, 15);
		}
		return label_1;
	}
	private JTextField getSubIn() {
		if (subIn == null) {
			subIn = new JTextField();
			subIn.setColumns(10);
			subIn.setBounds(76, 42, 322, 21);
		}
		return subIn;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(12, 70, 386, 280);
			scrollPane.setViewportView(getContentMsg());
		}
		return scrollPane;
	}
	private JButton getBtnSearch() {
		if (btnSearch == null) {
			btnSearch = new JButton("Search");
			btnSearch.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					int s = Integer.parseInt(serialIn.getText());
					
					
					select(s);
					
				}
			});
			btnSearch.setBounds(297, 10, 101, 23);
		}
		return btnSearch;
	}
	private JTextArea getContentMsg() {
		if (contentMsg == null) {
			contentMsg = new JTextArea();
		}
		return contentMsg;
	}
	private JButton getBtnMod() {
		if (btnMod == null) {
			btnMod = new JButton("Modify");
			btnMod.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					update();
				}
			});
		}
		return btnMod;
	}
	private JButton getBtnDel() {
		if (btnDel == null) {
			btnDel = new JButton("Delete");
			btnDel.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					delete();
				}
			});
		}
		return btnDel;
	}
	private JPanel getPanel_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			panel_1.setBounds(10, 356, 388, 50);
			panel_1.setLayout(new BorderLayout(0, 0));
			panel_1.add(getBtnMod(), BorderLayout.NORTH);
			panel_1.add(getBtnDel(), BorderLayout.SOUTH);
		}
		return panel_1;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("No");
			lblNewLabel.setBounds(189, 14, 24, 15);
		}
		return lblNewLabel;
	}
	private JTextField getSerialIn() {
		if (serialIn == null) {
			serialIn = new JTextField();
			serialIn.setBounds(225, 11, 60, 21);
			serialIn.setColumns(10);
		}
		return serialIn;
	}
	public void select(int a){
		
		try{
			String sql = "select * from guestbook where serial = ?";
			PreparedStatement pstmt = gbm.conn.prepareStatement(sql);
			pstmt.setInt(1, a);
			ResultSet rs = pstmt.executeQuery();
			if(rs.next()){
				serialIn.setText(""+rs.getInt("serial"));
				idIn.setText(rs.getString("mid"));
				subIn.setText(rs.getString("subject"));
				contentMsg.setText(rs.getString("content"));
			}else{
				JOptionPane.showMessageDialog(null, "찾는 데이터가 없습니다.");
				serialIn.setText("");
				idIn.setText("");
				subIn.setText("");
				contentMsg.setText("");
			}
			
			
			
		}catch(Exception ex){
			ex.printStackTrace();
		}
		
	}
	public void update(){
		String sql = "update guestbook set mid=?, subject=?, content=? where serial=?";
		
		try{
			// prepared statement 는 미완성된 sql 문을 말한다. 그것이 ? 이다.
			PreparedStatement pstmt = gbm.conn.prepareStatement(sql);
			pstmt.setString(1, idIn.getText());
			pstmt.setString(2, subIn.getText());
			pstmt.setString(3, contentMsg.getText());
			pstmt.setInt(4, Integer.parseInt(serialIn.getText()));
			
			pstmt.executeUpdate();				
			
			if(gbm.select!=null){
				gbm.select.setData();
			}			
			
		}catch(Exception ex){
			ex.printStackTrace();
		}	
		
		
	}
	public void delete(){
		String sql = "delete from guestbook where serial = ? ";
		
		try{
			PreparedStatement pstmt = gbm.conn.prepareStatement(sql);
			pstmt.setInt(1, Integer.parseInt(serialIn.getText()));
			pstmt.executeUpdate();
			
			serialIn.setText("");
			idIn.setText("");
			subIn.setText("");
			contentMsg.setText("");
			
			JOptionPane.showMessageDialog(null, "방명록이 삭제되었습니다.");
			
			if(gbm.select!=null){
				gbm.select.setData();
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
	
}
